﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace SpriteSheetKit
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            sp = new SpritePlayer(Sprites, PB_SelectionPreview , 250);
        }

        Bitmap SpriteSheet;

        List<Bitmap> Sprites = new List<Bitmap>();

        SpritePlayer sp;

        private void btnAdd_Click(object sender, EventArgs e)
        {
            sp.Stop();

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "png sprite image|*.png";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Sprites.Add(new Bitmap(ofd.FileName));
                listBox1.Items.Add(ofd.FileName);

                BuildSpriteSheet();
            }
        }

        private void BuildSpriteSheet()
        {
            sp.Stop();

            System.Drawing.Size spritesize = Sprites[0].Size;

            SpriteSheet = new Bitmap(spritesize.Width * Sprites.Count, spritesize.Height);

            labelSheetSize.Text = string.Format("{0} x {1}", spritesize.Width, spritesize.Height);
            labelSpriteSize.Text = string.Format("{0} x {1}", SpriteSheet.Width, SpriteSheet.Height);

            using (Graphics G = Graphics.FromImage(SpriteSheet))
            {
                for (int i = 0; i < Sprites.Count; i++)
                {
                    G.DrawImage(Sprites[i], new Point(i * spritesize.Width, 0));
                }
                
            }

            pictureBox1.Image = SpriteSheet;
            pictureBox1.Invalidate();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            sp.Stop();

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "PNG SpriteSheet|*.png";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SpriteSheet.Save(sfd.FileName, ImageFormat.Png);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sp.Stop();
            PB_SelectionPreview.Image = null;
            PB_SelectionPreview.Invalidate();

            Sprites.Clear();
            listBox1.Items.Clear();
            pictureBox1.Image = new Bitmap(5, 5);
            pictureBox1.Invalidate();

            labelSheetSize.Text = "0 x 0";
            labelSpriteSize.Text = "0 x 0";
        }

        private void BtnflipH_Click(object sender, EventArgs e)
        {
            sp.Stop();

            foreach (Bitmap b in Sprites)
            {
                b.RotateFlip(RotateFlipType.RotateNoneFlipX);
            }

            BuildSpriteSheet();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            sp.Stop();

            try
            {
                if (listBox1.SelectedIndex != -1)
                {
                    PB_SelectionPreview.Image = Image.FromFile(listBox1.SelectedItem.ToString());
                }
            }
            catch
            {

            }
        }


        private void btn_playSheet_Click(object sender, EventArgs e)
        {
            sp.Play();
        }

        private void TB_interval_Scroll(object sender, EventArgs e)
        {
            sp.FrameInterval = TB_interval.Value;
        }

        private void TB_interval_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                TB_interval.Value = 250;
                sp.FrameInterval = 250;
            }
        }
    }
}
